package com.example.testrecorder;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.testrecorder.domain.QueAndAns;
import com.example.testrecorder.domain.User;
import com.example.testrecorder.utils.LogUtil;

import org.litepal.LitePal;

import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/5/24 19:05
 * @version 1.0
 */
public class EditSQFragment extends Fragment {
    private EditText q1Et,q2Et,q3Et,a1Et,a2Et,a3Et;
    private Button saveBtn;
    private User currUser;
    private Toolbar toolbar;
    private static final String TAG = "EditSQFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_s_q, container, false);
        q1Et=view.findViewById(R.id.q1Et);
        q2Et=view.findViewById(R.id.q2Et);
        q3Et=view.findViewById(R.id.q3Et);
        a1Et=view.findViewById(R.id.a1Et);
        a2Et=view.findViewById(R.id.a2Et);
        a3Et=view.findViewById(R.id.a3Et);
        saveBtn=view.findViewById(R.id.saveBtn);
        currUser=MyApplication.currUser;
        List<QueAndAns> list = LitePal.where("user_id=?", "" + currUser.getId()).find(QueAndAns.class);
        LogUtil.logD(TAG,""+list.size());
        if(list.size()==3){
            QueAndAns queAndAns1 = list.get(0);
            QueAndAns queAndAns2 = list.get(1);
            QueAndAns queAndAns3 = list.get(2);
            q1Et.setText(queAndAns1.getQuestion());
            q2Et.setText(queAndAns2.getQuestion());
            q3Et.setText(queAndAns3.getQuestion());
            a1Et.setText(queAndAns1.getAnswer());
            a2Et.setText(queAndAns2.getAnswer());
            a3Et.setText(queAndAns3.getAnswer());
            q1Et.requestFocus();//获得焦点

        }
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(q1Et.getText().toString())||TextUtils.isEmpty(q2Et.getText().toString())
                ||TextUtils.isEmpty(q3Et.getText().toString())||TextUtils.isEmpty(a1Et.getText().toString())
                ||TextUtils.isEmpty(a2Et.getText().toString())||TextUtils.isEmpty(a3Et.getText().toString())){
                    Toast.makeText(getActivity(),"保存失败，请完整填写3个密保问题和答案！",Toast.LENGTH_SHORT).show();
                    return;
                }
                List<QueAndAns> list2 = LitePal.where("user_id=?", "" + currUser.getId()).find(QueAndAns.class);
                if(list2.size()==0){//如果用户从来没有设置过密保
                    QueAndAns queAndAns1=new QueAndAns(q1Et.getText().toString(),a1Et.getText().toString());
                    queAndAns1.setUser_id(currUser.getId());
                    queAndAns1.save();

                    QueAndAns queAndAns2=new QueAndAns(q2Et.getText().toString(),a2Et.getText().toString());
                    queAndAns2.setUser_id(currUser.getId());
                    queAndAns2.save();

                    QueAndAns queAndAns3=new QueAndAns(q3Et.getText().toString(),a3Et.getText().toString());
                    queAndAns3.setUser_id(currUser.getId());
                    queAndAns3.save();

                    Toast.makeText(getActivity(),"保存成功！",Toast.LENGTH_SHORT).show();
                    back();
                }else if(list2.size()==3){//修改密保
                    QueAndAns queAndAns1 = list2.get(0);
                    queAndAns1.setQuestion(q1Et.getText().toString());
                    queAndAns1.setAnswer(a1Et.getText().toString());
                    queAndAns1.save();

                    QueAndAns queAndAns2 = list2.get(1);
                    queAndAns2.setQuestion(q2Et.getText().toString());
                    queAndAns2.setAnswer(a2Et.getText().toString());
                    queAndAns2.save();

                    QueAndAns queAndAns3 = list2.get(2);
                    queAndAns3.setQuestion(q3Et.getText().toString());
                    queAndAns3.setAnswer(a3Et.getText().toString());
                    queAndAns3.save();

                    Toast.makeText(getActivity(),"保存成功！",Toast.LENGTH_SHORT).show();
                    back();

                }else {
                    Toast.makeText(getActivity(),"当前用户的密保个数异常！",Toast.LENGTH_SHORT).show();
                }


            }
        });
        toolbar=view.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }

        });

        return view;
    }
    private void back() {
        FragmentActivity activity=getActivity();
        if(activity!=null){
            activity.onBackPressed();//调用活动的返回键
        }
    }
}